// PollutionAnalysisController.java
package com.chinasoft.pollution_analysis.controller;

import com.chinasoft.pollution_analysis.entity.*;
import com.chinasoft.pollution_analysis.service.PollutionAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/pollution")
@CrossOrigin(origins = "*")
public class PollutionAnalysisController {

    @Autowired
    private PollutionAnalysisService pollutionAnalysisService;

    /**
     * 获取全国污染物平均浓度统计
     */
    @GetMapping("/national-stats")
    public ApiResponse<List<PollutionStats>> getNationalStats() {
        try {
            List<PollutionStats> data = pollutionAnalysisService.getNationalStats();
            return ApiResponse.success(data);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取全国统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取区域污染物浓度统计
     */
    @GetMapping("/regional-stats")
    public ApiResponse<List<RegionalStats>> getRegionalStats(
            @RequestParam(value = "city", required = false) String city) {
        try {
            List<RegionalStats> data = pollutionAnalysisService.getRegionalStats(city);
            return ApiResponse.success(data);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取区域统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取时间趋势数据
     */
    @GetMapping("/time-trends/{timeUnit}")
    public ApiResponse<List<TimeTrend>> getTimeTrends(
            @PathVariable String timeUnit,
            @RequestParam(value = "pollutant", required = false) String pollutantType) {
        try {
            List<TimeTrend> data = pollutionAnalysisService.getTimeTrends(timeUnit, pollutantType);
            return ApiResponse.success(data);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取时间趋势数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取城市污染排名
     */
    @GetMapping("/city-rankings")
    public ApiResponse<List<CityRanking>> getCityRankings(
            @RequestParam(value = "pollutant", required = false) String pollutantType,
            @RequestParam(value = "limit", defaultValue = "50") Integer limit) {
        try {
            List<CityRanking> data = pollutionAnalysisService.getCityRankings(pollutantType, limit);
            return ApiResponse.success(data);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取城市排名数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取重点城市分析
     */
    @GetMapping("/key-city-analysis")
    public ApiResponse<List<RegionalStats>> getKeyCityAnalysis() {
        try {
            List<RegionalStats> data = pollutionAnalysisService.getKeyCityAnalysis();
            return ApiResponse.success(data);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取重点城市分析数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取污染物分布
     */
    @GetMapping("/pollutant-distribution")
    public ApiResponse<List<PollutantDistribution>> getPollutantDistribution() {
        try {
            List<PollutantDistribution> data = pollutionAnalysisService.getPollutantDistribution();
            return ApiResponse.success(data);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取污染物分布数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取热点分析数据
     */
    @GetMapping("/hotspot-analysis")
    public ApiResponse<List<HotspotAnalysis>> getHotspotAnalysis(
            @RequestParam(value = "date", required = false) String date,
            @RequestParam(value = "region", required = false) String region) {
        try {
            List<HotspotAnalysis> data = pollutionAnalysisService.getHotspotAnalysis(date, region);
            return ApiResponse.success(data);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取热点分析数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取时空趋势数据
     */
    @GetMapping("/spatio-temporal-trend")
    public ApiResponse<List<SpatioTemporalTrend>> getSpatioTemporalTrend(
            @RequestParam(value = "region", required = false) String region,
            @RequestParam(value = "season", required = false) String season) {
        try {
            List<SpatioTemporalTrend> data = pollutionAnalysisService.getSpatioTemporalTrend(region, season);
            return ApiResponse.success(data);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取时空趋势数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取实时统计信息
     */
    @GetMapping("/real-time-stats")
    public ApiResponse<Map<String, Object>> getRealTimeStats() {
        try {
            Map<String, Object> data = pollutionAnalysisService.getRealTimeStats();
            return ApiResponse.success(data);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取实时统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取污染物类型列表
     */
    @GetMapping("/pollutant-types")
    public ApiResponse<List<String>> getPollutantTypes() {
        try {
            List<String> data = pollutionAnalysisService.getPollutantTypes();
            return ApiResponse.success(data);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取污染物类型列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取城市列表
     */
    @GetMapping("/cities")
    public ApiResponse<List<String>> getCityList() {
        try {
            List<String> data = pollutionAnalysisService.getCityList();
            return ApiResponse.success(data);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取城市列表失败: " + e.getMessage());
        }
    }
}